package cn.sh.tools.service.schedule;

import cn.sh.tools.service.component.mail.MailComponent;
import cn.sh.tools.service.component.wechat.BusinessWeChatComponent;
import cn.sh.tools.util.date.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author shihao
 * @description 定时任务
 */
@Component
@Slf4j
public class CommonSchedule {

    /**
     * 邮件组件
     */
    @Resource
    private MailComponent mailComponent;

    @Autowired
    private BusinessWeChatComponent businessWeChatComponent;

    private static List<String> overtimeWeekList;

    static {
        overtimeWeekList = new ArrayList<>();

        overtimeWeekList.add("星期一");
        overtimeWeekList.add("星期五");
        overtimeWeekList.add("星期六");
    }

    /**
     * 每天七点发送邮件，提醒提交加班单
     */
    @Scheduled(cron = "0 0 9 * * ?")
    public void sendMailTask() {
        log.info("每天九点定时预备发送加班单提醒邮件");

        String week = DateUtils.thisDayOfWeekEnum().toChinese();
        if (overtimeWeekList.contains(week)) {
            log.info("正式发送加班单提醒邮件");

            String content = "是不是又加班了呢？赶紧提交加班单吧，毕竟工作已经很辛苦了，就不要再被白嫖了。。。生活虽苦，但要开心！！！";
            //this.mailComponent.sendSimpleMail(null, null, "加班单提醒", content);
            this.businessWeChatComponent.msgPut("", content);
        }
    }

    /**
     * 每分钟执行一次
     */
//    @Scheduled(cron = "0 * * * * ?")
    public void testTask() {
        String now = DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        log.info("当前系统时间为：[{}], 当前生成的随机数为: [{}]", now, Math.random());
        log.info("====================");
    }

}
